package org.mogoo.simple.socket2;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * Author: Fang
 * Date: 2012-08-21
 * Time: 19:49
 * Email: dongfang@mogoo.org
 */
public class Client2
{
    private Socket socket = null;
    OutputStream outputStream = null;
    InputStream inputStream = null;

    public static void main(String[] args) throws Exception
    {
        new Client2().startClient();
    }

    public void startClient() throws Exception
    {
        socket = new Socket("127.0.0.1", 8888);
        login();
        receiveMessage();
    }

    public void login() throws Exception
    {

        outputStream = socket.getOutputStream();

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        RequestBody requestBody = new RequestBody();
        requestBody.setRequestType(RequestType.login);
        requestBody.setClientID(2);
        requestBody.setDestinationID(1);
        objectOutputStream.writeObject(requestBody);


    }

    public void receiveMessage() throws Exception
    {
        inputStream = socket.getInputStream();
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);

        while(true)
        {
            RequestBody requestBody = (RequestBody)objectInputStream.readObject();

            System.out.println(requestBody.getMessageBody());
        }
        //objectInputStream.close();
    }
}
